'''
@文件    :views.py
@说明    : 
@时间    :2020/04/03 15:48:25
@作者    :didiplus
@版本    :1.0
'''

from flask import Blueprint,make_response,jsonify,json,request
from .__init__ import Send,request_query,BASE_URL

artists = Blueprint('artists',__name__,url_prefix='/api/artists')


#获取歌手单曲
@artists.route('/song',methods=['POST','GET'])
def song():
    query = request_query(request,'id')
    data = Send({}).POST('weapi/v1/artist/' + query['id'])

    return make_response(jsonify(json.loads(data.text)),403)

#歌手专辑
@artists.route('/album',methods=['POST','GET'])
def album():
    query = request_query(request,'id',['limit',{'limit':30}],['offset',{'offset':0}])
    query['total'] = True
    id = query.pop('id')
    data = Send(query).POST('weapi/artist/albums/' + id)
    return make_response(jsonify(json.loads(data.text)),403)

#最新专辑
@artists.route('/album/new',methods=['POST','GET'])
def newAlbum():
    data = Send().POST("weapi/discovery/newAlbum")
    return make_response(jsonify(json.loads(data.text)),403)


#获取专辑的内容
@artists.route('/album/detail',methods=['POST','GET'])
def album_detail():
    query = request_query(request,'id')
    data = Send().POST("weapi/v1/album/"+query["id"])
    return make_response(jsonify(json.loads(data.text)),403)

#专辑动态信息
@artists.route('/album/dynamic',methods=['POST','GET'])
def dynamic():
    query = request_query(request,'id')
    data = Send(query).POST("weapi/album/detail/dynamic")
    return make_response(jsonify(json.loads(data.text)),403)

# 收藏/取消收藏专辑
@artists.route('/album/sub',methods=['POST','GET'])
def album_sub():
    query = request_query(request, "id", ["t", {"t": 1}])
    t = "sub" if query.pop("t") == "1" else "unsub"
    data = send(query).POST("weapi/album/" + t)
    return make_response(jsonify(json.loads(data.text)),403)

# 获取已收藏专辑列表
@artists.route('/album/sublist',methods=['POST','GET'])
def album_sublist():
    query = request_query(request,
                          ["limit", {"limit": 30}],
                          ["offset", {"offset": 0}])
    query["total"] = True
    data = Send(query).POST("weapi/album/sublist")
    return make_response(jsonify(json.loads(data.text)),403)

#获取歌手详情
@artists.route('/desc',methods=['POST','GET'])
def desc():
    query = request_query(request, 'id')
    data = Send(query).POST('weapi/artist/introduction')
    return make_response(jsonify(json.loads(data.text)),403)


#歌手MV
@artists.route('/mv',methods=['POST','GET'])
def mv():
    query = request_query(request,
                          ['id', 'artistId'],
                          ['limit', {'limit': 30}],
                          ['offset', {'offset': 0}])
    query['total'] = True
    data = Send(query).POST('weapi/artist/mvs')
    return make_response(jsonify(json.loads(data.text)),403)

#热门歌手
@artists.route('/top',methods=['POST','GET'])
def top():
    query = request_query(request,
                          ['limit', {'limit': 30}],
                          ['offset', {'offset': 0}])
    query["total"] = True
    data = Send(query).POST("weapi/artist/top")
    return make_response(jsonify(json.loads(data.text)),403)

# 获取相似歌手
@artists.route('/simi',methods=['POST','GET'])
def simi():
    query = request_query(request, ["id", "artistid"])
    data = Send(query).POST("weapi/discovery/simiArtist")
    return make_response(jsonify(json.loads(data.text)),403)

# 歌手分类列表
@artists.route('/lists',methods=['POST','GET'])
def lists():
    query = request_query(request,
                          ["cat", {"categoryCode": 1001}],
                          ["init", {"initial": ""}],
                          ['limit', {'limit': 30}],
                          ['offset', {'offset': 0}])
    try:
        query["initial"] = ord(query["initial"].upper())
    except:
        pass
    data = Send(query).POST("weapi/artist/list")
    return make_response(jsonify(json.loads(data.text)),403)

# 收藏/取消收藏歌手
@artists.route('/sub',methods=['POST','GET'])
def sub():
    query = request_query(request,
                          ["t", {"t": 1}],
                          ["id", "artistId"])
    query["artistIds"] = "[{}]".format(query["artistId"])
    t = "sub" if query.pop("t") == "1" else "unsub"
    data = Send(query).POST("weapi/artist/"+t)
    return make_response(jsonify(json.loads(data.text)),403)

# 收藏的歌手列表
@artists.route('/sublist',methods=['POST','GET'])
def sublist():
    query = request_query(request,
                          ['limit', {'limit': 30}],
                          ['offset', {'offset': 0}])
    query["total"] = True
    data = Send(query).POST("weapi/artist/sublist")
    return make_response(jsonify(json.loads(data.text)),403)

# 热门50首歌
@artists.route('/topsong',methods=['POST','GET'])
def topsong(request):
    
    query = request_query(request, "id")
    data = Send(query).POST("weapi/artist/top/song")
    return make_response(jsonify(json.loads(data.text)),403)

# 歌手榜
@artists.route('/toplist',methods=['POST','GET'])
def toplist():
    query = {"type": 1, "limit": 100, "offset": 0, "total": True}
    data = Send(query).POST("weapi/toplist/artist")
    return make_response(jsonify(json.loads(data.text)),403)